/* edited by wildw1ng */

/* clientside message */
_takenmessage =
{
	_msg1 = _this select 1;
	_msg2 = _this select 1;
	playSound "hint";
	Server globalChat format ["%1 has been captured by %2.", (_msg1 select 0) getVariable "name",(_msg2 select 0) getVariable "side"];
};
"LIB_mp_taken" addPublicVariableEventHandler _takenmessage;

/* side colors */
LIB_mp_sidecolors = ["ColorBLUFOR", "ColorOPFOR", "ColorIndependent", "ColorWhite"];

/* flag marker icon */
_flagmarkertype = "mil_flag";
_flagmarkersize = [0.8, 0.8];

/* flags */
LIB_mp_flags_pool = [];
{
	_flag = _x;
	if (typeOf _flag in ["FlagPole_F", "FlagBlue", "FlagRed", "FlagGreen"]) then
	{
		LIB_mp_flags_pool set [count LIB_mp_flags_pool, _flag];
		_flag setVariable ["flagz", getPos _flag select 2];
	};
} forEach allMissionObjects "FlagPole_F";

if (isServer) then {
	_idx = floor(random(count LIB_mp_flags_pool));
	if (!isnil "Conqu3st_SiteSelection" && {Conqu3st_SiteSelection >= 0}) then {
		{
			if ((_x getVariable ["uid",-1]) == Conqu3st_SiteSelection) exitWith {
				_idx = _forEachIndex;
			};
		} foreach LIB_mp_flags_pool;
	};
	LIB_mp_flags = [(LIB_mp_flags_pool select _idx)];
	publicVariable "LIB_mp_flags";
	server setVariable ["LIB_mp_flags",LIB_mp_flags, true];
}	else {
	waituntil {!isnil "LIB_mp_flags" || (typename(server getVariable ["LIB_mp_flags",objNull]) != typename objNull)};
	if (isnil "LIB_mp_flags" || {count LIB_mp_flags <= 0}) then {
		LIB_mp_flags = server getVariable ["LIB_mp_flags",[]];
	};
};

{
	_flag = _x;
	if (typeOf _flag in ["FlagPole_F", "FlagBlue", "FlagRed", "FlagGreen"]) then
	{
		/* name */
		if (isNil {_flag getVariable "name"}) then
		{
			_flagname = "Flag";
			if (count LIB_mp_flags > 1) then {
				_flagname = _flagname + " " + str (_forEachIndex + 1);
			};
			_flag setVariable ["name", _flagname];
		};
		/* name marker */
		_namemarker = createMarkerLocal [format ["sectorName%1", _forEachIndex + 1], getPos _flag];
		_namemarker setMarkerTypeLocal _flagmarkertype;
		_namemarker setMarkerSizeLocal _flagmarkersize;
		_flag setVariable ["namemarker", _namemarker];
		if (_flag getVariable ["showname", true]) then
		{
			_namemarker setMarkerTextLocal (_flag getVariable "name");
		};
		_color = "ColorWhite";
		if (!LIB_mp_flagsneutral) then
		{
			switch (typeOf _x) do
			{
				case "FlagBlue" : {_color = "ColorBLUFOR";};
				case "FlagRed" : {_color = "ColorOPFOR";};
				case "FlagGreen" : {_color = "ColorIndependent";};
			};
		};
		_namemarker setMarkerColorLocal _color;
	};
} forEach LIB_mp_flags;

[] spawn
{
	while {true} do
	{
		sleep 0.1;
		{
			_color = "ColorWhite";
			if (_x getVariable "side" == west) then {_color = "ColorBLUFOR";};
			if (_x getVariable "side" == east) then {_color = "ColorOPFOR";};
			if (_x getVariable "side" == resistance) then {_color = "ColorIndependent";};
			(_x getVariable "namemarker") setMarkerColorLocal _color;
		} forEach LIB_mp_flags;
	};
};

/* server only */
if (!isServer) exitWith {};


/* init score array */
LIB_mp_scores = [];
{
	LIB_mp_scores set [count LIB_mp_scores, 0];
} forEach LIB_mp_activesides;
publicVariable "LIB_mp_scores";

/* marker refresh */
LIB_mp_markers = [];
[] spawn
{
	while {true} do
	{
		sleep 3;
		{
			_x setMarkerAlpha markerAlpha _x;
		} forEach LIB_mp_markers;
	};
};

/* yield time */
_yieldtime = LIB_mp_yieldtime;

/* determine side and create markers */
{
	_flag = _x;
	/* sector */
	_sector = createTrigger ["EmptyDetector", getPosATL _flag];
	_sector setTriggerArea [LIB_mp_sectorsize, LIB_mp_sectorsize, 0, false];
	_sector setTriggerActivation ["ANY", "PRESENT", true];
	_sector setTriggerStatements ["this", "", ""];
	_flag setVariable ["sector", _sector];
	_flag setVariable ["seizetime", LIB_mp_seizetime, true];
	/* sector marker */
	_marker = createMarker [format ["sectorMarker%1", _forEachIndex + 1], getPos _flag];
	_marker setMarkerShape "ELLIPSE";
	_marker setMarkerBrush "Solid";
	_marker setMarkerSize [triggerArea _sector select 0, triggerArea _sector select 1];
	LIB_mp_markers set [count LIB_mp_markers, _marker];
	_flag setVariable ["marker", _marker];
	/* sector border marker */
	_bordermarker = createMarker [format ["sector%1border", _forEachIndex + 1], getPos _flag];
	_bordermarker setMarkerShape "ELLIPSE";
	_bordermarker setMarkerBrush "Border";
	_bordermarker setMarkerSize [(triggerArea _sector select 0) + 0.2, (triggerArea _sector select 1) + 0.2];
	/* flag owner and texture */
	_side = sideEnemy;
	if (!LIB_mp_flagsneutral) then
	{
		switch (typeOf _x) do
		{
			case "FlagBlue" : {_side = west;};
			case "FlagRed" : {_side = east;};
			case "FlagGreen" : {_side = resistance;};
		};
	};
	_color = "ColorWhite";
	_texture = "a3\data_f\Flags\flag_white_co.paa";
	if (_side != sideEnemy) then
	{
		_color = LIB_mp_sidecolors select (LIB_mp_activesides find _side);
		_texture = LIB_mp_flagtextures select (LIB_mp_activesides find _side);
	};
	_flag setVariable ["side", _side, true];
	_marker setMarkerColor _color;
	_flag setFlagTexture _texture;
} forEach LIB_mp_flags;

_emptytimer = 0;

/* loop */
_step = 0.2;
_wait = _step;
while {true} do
{
	waitUntil {time >= _wait + _step && !LIB_mp_cutscene};
	_wait = _wait + _step;
	{
		_flag = _x;
		_sector = _flag getVariable "sector";
		_side = _flag getVariable "side";
		_marker = _flag getVariable "marker";
		_seizetime = _flag getVariable "seizetime";
		/* relocate flag if building under it collapses */
		if (getPos _flag select 2 > (_flag getVariable "flagz")) then
		{
			_flag setPos [getPos _flag select 0, getPos _flag select 1, (getPosATL _flag select 2) - (getPos _flag select 2)];
			_flag setVariable ["flagz", getPos _flag select 2];
		};
		/* sort units by their side */
		_dominantside = sideEnemy;
		_unitsperside = [];
		_dominationperside = [];
		if (count list _sector > 0) then
		{
			{
				_unitsperside set [count _unitsperside, []];
				_dominationperside set [count _dominationperside, 0];
			} forEach LIB_mp_activesides;
			{
				_unit = _x;
				if (alive _unit && side _unit in LIB_mp_activesides && vehicle _unit == _unit) then
				{
					_index = LIB_mp_activesides find side _unit;
					_units = [];
					if (_unit isKindOf "Man") then
					{
						_units = [_unit];
					};
					_unitsperside set [_index, (_unitsperside select _index) + _units];
					_dominationperside set [_index, (_dominationperside select _index) + count _units];
				};
			} forEach list _sector;
			_sorted = (+ _dominationperside) call BIS_fnc_sortNum;
			_maxindex = (count _sorted) - 1;
			if ((_sorted select _maxindex) > (_sorted select (_maxindex - 1))) then
			{
				_dominantside = LIB_mp_activesides select (_dominationperside find (_sorted select _maxindex));
				if (_dominantside != _side) then
				{
					_seizetime = (_seizetime - _step) max 0;
				}
				else
				{
					if (_emptytimer >= 3) then
					{
						_seizetime = LIB_mp_seizetime;
						_emptytimer = 0;
					}
					else
					{
						_emptytimer = _emptytimer + _step;
					};
				};
				_flag setVariable ["seizetime", _seizetime, true];
				_multiplier = _seizetime / LIB_mp_seizetime;
				_marker setMarkerSize [(triggerArea _sector select 0) * _multiplier, (triggerArea _sector select 1) * _multiplier];
			};
		};
		if ({_x == 0} count _dominationperside == count _dominationperside) then
		{
			if (_seizetime != LIB_mp_seizetime) then
			{
				if (_emptytimer >= 3) then
				{
					_seizetime = LIB_mp_seizetime;
					_flag setVariable ["seizetime", _seizetime, true];
					_marker setMarkerSize [triggerArea _sector select 0, triggerArea _sector select 1];
					_emptytimer = 0;
				}
				else
				{
					_emptytimer = _emptytimer + _step;
				};
			};
		};
		/* flag captured */
		if (_flag getVariable "seizetime" == 0 && _dominantside != _side) then
		{
			_side = _dominantside;
			_flag setVariable ["side", _side, true];
			_index = LIB_mp_activesides find _side;
			_sidecolor = LIB_mp_sidecolors select _index;
			LIB_mp_taken = [_flag, _side];
			publicVariable "LIB_mp_taken";
			[[], LIB_mp_taken] call _takenmessage;
			_flag setVariable ["seizetime", LIB_mp_seizetime, true];
			_marker setMarkerSize [triggerArea _sector select 0, triggerArea _sector select 1];
			_flag setFlagTexture (LIB_mp_flagtextures select _index);
			{_flag getVariable _x setMarkerColor _sidecolor;} forEach ["marker"];
			{_x addScore 5;} forEach (_unitsperside select (LIB_mp_activesides find _side));
		};
	} forEach LIB_mp_flags;
	/* score yield */
	if (time >= _yieldtime) then
	{
		{
			_side = _x;
			_yield = (({_x getVariable "side" == _side} count LIB_mp_flags)) max 0;
			LIB_mp_scores set [_forEachIndex, (LIB_mp_scores select _forEachIndex) + _yield];
			publicVariable "LIB_mp_scores";
		} forEach LIB_mp_activesides;
		_yieldtime = _yieldtime + LIB_mp_yieldtime;
	};
};